/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Advertentie;
import model.Lid;
import model.Rit;
import org.hibernate.Session;
import org.hibernate.Transaction;
import persistence.SessionManager;

/**
 *
 * @author Johan
 */
public class AdvertentieOverzichtController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // Haal de huidige sessie op
        HttpSession sessie = request.getSession();

        //huidige lid uit de sessie halen
        Lid lid = (Lid) sessie.getAttribute("currentUser");

        String hql = "from Advertentie where ADV_DELETED = false AND LID_ID = " + lid.getLidId();

        Session session = null;
        session = SessionManager.openSession();

//        Transaction tx = session.beginTransaction();
        List<Advertentie> advertenties = session.createQuery(hql).list();
        session.close();

        // Array(list) meesturen naar advertentieoverzicht 
        request.setAttribute("advertenties", advertenties);
        request.setAttribute("aantalAdvertenties", advertenties.size());

        String address = "/mijnAdverteer.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }
}